package cn.tuling.nettyadv.client;

import cn.tuling.nettyadv.vo.MyMessage;
import io.netty.channel.*;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Mark老师
 * 类说明：接收业务应答消息并处理
 */
public class ClientBusiHandler extends SimpleChannelInboundHandler<MyMessage> {

    private static final Logger LOG = LoggerFactory.getLogger(ClientBusiHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyMessage msg) throws Exception {
        LOG.info("业务应答消息："+msg.toString());
    }

    /**
     * Calls {@link ChannelHandlerContext#fireUserEventTriggered(Object)} to forward
     * to the next {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
     * <p>
     * Sub-classes may override this method to change behavior.
     *
     * @param ctx
     * @param evt
     */
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

       /* if (evt instanceof IdlestateEvent){
            IdlestateEvent idlestateEvent (IdleStateEvent)evt;
            if (idlestateEvent.state()=Idlestate.WRITER_IDLE){
                TIMReqMsg heartBeat SpringBeanFactory.getBean(name:"heartBeat",TIMReqMsg.class);
//System.out,printin("客户端给服务端发送心跳")；
                ctx.writeAndFlush(heartBeat).addListeners((ChannelFutureListener) future ->
                if (!future.isSuccess()) {
                    LOGGER.error("I0 error,close Channel");
                    future.channel().close();
                }
            }
        }*/
        super.userEventTriggered(ctx, evt);
    }
}
